var $;
var table;
var layer;
var form;
var upload;
var arr2=[];
var a=0;
var element;
var delImg=[];
var noDelImg=[];
layui.use(['table','element','layer','upload'],function(){
    $=layui.jquery;
    table=layui.table;
    layer=layui.layer;
    form=layui.form;
    upload=layui.upload;
    element=layui.element;

    var course=$("#courseid").val();
    SeCourseView(course);

    //修改视频
    var uploadInst3 = upload.render({
        elem: '#upVideo' //绑定元素
        ,url: '/SyteacherSelController/uploadAttachments' //上传接口
        ,done: function(res, index, upload){

            //截取查询出，删除视频
            var url=$(".uploadVideo").attr("src");
            var delUrl=url.substr(url.lastIndexOf(':')+2);
            var jsonUrl={"delImg":delUrl};
            delImg.push(jsonUrl);

            var arrayImgs=res.src;
            var courseUrl=arrayImgs.substr(arrayImgs.lastIndexOf(':')+2);
            $("#courseUrl").val(courseUrl);

            $("#my-video").remove();

            $("#td_video").append(" " +
                "<video id='my-video' class='video-js' controls preload='auto' data-setup='{}'> " +
                "<source src='"+res.src+"' class='uploadVideo' type='video/mp4'/> " +
                "</video>"
            );



            element.progress('progressBar',100 + '%');
            setTimeout("fun()","2000");  //2000毫秒后执行test()函数，只执行一次。

            //截取查询出，删除视频
            var url2=$(".uploadVideo").attr("src");
            var delUrl2=url2.substr(url2.lastIndexOf(':')+2);
            var jsonUrl2={"delImg":delUrl2};
            noDelImg.push(jsonUrl2);

            layer.msg("修改成功", {icon:1});
        }
        ,error: function(){
            //请求异常回调
        },
        progress: function(value){
            $("#progressBar").attr("style","display:block");
            if(value<99){
                element.progress('progressBar',value + '%');
            }
        },
        drag : true,
        accept : 'video',
        multiple : true
    });

    //修改主图
    var uploadInst = upload.render({
        elem: '#test1' //绑定元素
        ,url: '/SyteacherSelController/uploadAttachments' //上传接口
        ,done: function(res, index, upload){

            //截取查询出，删除图片
            var url=$("#img").attr("src");
            var delUrl=url.substr(url.lastIndexOf(':')+2);
            var jsonUrl={"delImg":delUrl};
            delImg.push(jsonUrl);

            $("#url").val(res.src);
            $("#img").remove();
            $("#imgpid").append("<img id='img' src='"+res.src+"' style='width: 250px;height: 120px'>");
            layer.msg("修改成功", {icon:1});

            //截取查询出，删除图片
            var url2=$("#img").attr("src");
            var delUrl2=url2.substr(url2.lastIndexOf(':')+2);
            var jsonUrl2={"delImg":delUrl2};
            noDelImg.push(jsonUrl2);

        }
        ,error: function(){
            //请求异常回调
        }
    });

    //添加预览图
    var uploadInst2 = upload.render({
        elem: '#test2' //绑定元素
        ,url: '/SyteacherSelController/uploadAttachments' //上传接口
        ,done: function(res, index, upload){
            //截取添加到数组
            var arrayImgs=res.src;
            var url=arrayImgs.substr(arrayImgs.lastIndexOf(':')+2);

            var jsonUrl2={"delImg":url};
            noDelImg.push(jsonUrl2);

            //获取当前登陆人员的信息
            var create=localStorage.getItem("user");
            create=JSON.parse(create);//将获取json字符串转json对象

            var courseId=$("#courseid").val();
            var courseName=$("#courseName").val();

            $("#tdImg").append("<p id='pId'><img src='"+res.src+"'class='microcircleImg'/><i class='layui-icon layui-icon-close' id='iId' value='"+a+"'  onclick='deleteBtn2(this,\""+res.src+"\")' title='删除图片'></i></p>");


            var json={"pictureUrl":url,"num":a,"createUser":create.user_id,"pictureType":2,"courseId":courseId,"courseName":courseName};
            arr2.push(json);

            //每次添加a都加一，用于删除时获取下标
            a++;
        }
        ,error: function(){
            //请求异常回调
        }
    });

    function SeCourseView(id){
        $.ajax({
            url:"/SyteacherSelController/SeCourseView",
            type:"post",
            dataType:"json",
            data:{"courseid":id},
            success:function(data){
                var url=data.data.url;
                var delUrl=url.substr(url.lastIndexOf(':')+2);

                var arrayImgs=data.data.courseUrl;
                var courseUrl=arrayImgs.substr(arrayImgs.lastIndexOf(':')+2);


                //循环预览图
                for(var a=0;a<data.data.microcircleImg.length;a++){


                    $("#tdImg").append("<p id='pId'><img src='"+data.data.microcircleImg[a].picture_url+"'class='microcircleImg'/> <i class='layui-icon layui-icon-close' id='iId'onclick='deleteBtn(this,\""+data.data.microcircleImg[a].picture_url+"\")' value='"+data.data.microcircleImg[a].picture_id+"' title='删除图片'></i></p>")
                }

                //视频
                $("#td_video").append(" " +
                    "<video id='my-video' class='video-js' controls preload='auto' data-setup='{}'> " +
                    "<source src='"+data.data.courseUrl+"' class='uploadVideo' type='video/mp4'/> " +
                    "</video>"
                );
                //截取视频路径

                $("#courseUrl").val(courseUrl);
                $("#courseName").val(data.data.courseName);

                var date=data.data.createDate;
                date = date.slice(0, date.indexOf("."));
                date = date.replaceAll('T',' ');
                $("#createDate").val(date);
                $("#createName").val(data.data.userName);
                $("#courseNprice").val(data.data.courseNprice);
                $("#coursePrice").val(data.data.courseNprice);
                $("#coursePrimary").val(data.data.coursePrimary);
                $("#courseCatory").val(data.data.className);
                $("#likNum").val(data.data.likeNum);
                $("#collectNum").val(data.data.collectNum);
                $("#shareNum").val(data.data.saleNum);
                $("#remarks").val(data.data.remarks);
                $("#courseIntro").val(data.data.courseIntro);
                $("#imgpid").html("<img id='img' src='"+data.data.url+"'style='width: 250px;'>");
                $("#url").val(data.data.url);

            }
        })
    }

    var uploadInst2 = upload.render({
        elem: '#iUpd' //绑定元素
        ,url: '/SyteacherSelController/uploadAttachments' //上传接口
        ,done: function(res, index, upload){
            $("#url").val(res.src);
            $("#img").remove();
            $("#imgpid").append("<img id='img' src='"+res.src+"' style='width: 250px;height: 120px'>");
        }
        ,error: function(){
            //请求异常回调
        }
    });
});

function selCouCategory() {
    layer.open({
        type:2,
        title:"分类详情",
        area:['600px','400px'],
        shade:0.4,
        offset:'auto',
        content:"TreeView.html",
        success:function(layero,index){
        }
    });
    window.layer_2=layer;
}
//课程修改
var arr=[];
function determine(){
    var courseName= $("#courseName").val();
    var courseNprice=$("#courseNprice").val();
    var coursePrimary=$("#coursePrimary").val();
    var classId=$("#courseCatoryId").val();
    var courseIntro=$("#courseIntro").val();
    var remarks=$("#remarks").val();
    //截取课程图片
    var arrayImgs=$("#url").val();
    var url=arrayImgs.substr(arrayImgs.lastIndexOf(':')+2);
    var courseId=$("#courseid").val();
    var coursePrice=$("#coursePrice").val();
    var courseUrl=$("#courseUrl").val();
    //获取当前登陆人员的信息
    var create=localStorage.getItem("user");
    create=JSON.parse(create);//将获取json字符串转json对象
    var formData = {"courseName":courseName,"courseNprice":courseNprice,"coursePrimary":coursePrimary,"classId":classId,"courseIntro":courseIntro,"remarks":remarks,"url":url,"courseId":courseId,"coursePrice":coursePrice,"createId": create.user_id,"updateId":create.user_id,"courseUrl":courseUrl};
    formData.str=JSON.stringify(arr);
    formData.strAdd=JSON.stringify(arr2);

    //删除服务器课程主图
    formData.delImg=JSON.stringify(delImg);

    $.ajax({
        url:"/SyteacherSelController/Upcourse",
        type:"post",
        dataType:"json",
        traditional: true,
        data:formData,
        success:function(data){
            if(data.code==0){
                parent.location.reload();
                var layer_1=parent.layer_1;
                layer_1.close(layer_1.index);
            }else{
                layer.msg('修改失败');
            }
        }
    });
}

function fun() {
    $("#progressBar").attr("style","display:none");
}

//取消并删除服务器上传多余图片
function cancels() {
    if(noDelImg.length>0){
        $.ajax({
            url:"/SyteacherSelController/noDelImg",
            type:"post",
            dataType:"json",
            traditional: true,
            data:{"noDelImg":JSON.stringify(noDelImg)},
            success:function(data){
                var layer_1=parent.layer_1;
                layer_1.close(layer_1.index);
            }
        });
    }else{
        var layer_1=parent.layer_1;
        layer_1.close(layer_1.index);
    }
}

//点击删除，把预览图id放入数组
var deleteBtn = function(obj,url){
    var arrayImgs=url;
    var delUrl=arrayImgs.substr(arrayImgs.lastIndexOf(':')+2);
    var json={"delImg":delUrl};
    delImg.push(json);


    obj.parentElement.remove();
    var id = obj.getAttribute('value');
    json={"pictureId":id};
    arr.push(json);
 };

//添加时，删掉预览图片
var deleteBtn2 = function(obj,url){
    var arrayImgs=url;
    var delUrl=arrayImgs.substr(arrayImgs.lastIndexOf(':')+2);

    var json={"delImg":delUrl};
    noDelImg.push(json);

    obj.parentElement.remove();
    var id = obj.getAttribute('value');
    //根据num值，获取下标，进行删除
    var index=arr2.findIndex((element)=>(element.num ==id));
    arr2.splice(index,1);
};
